<template>
  <div class="h-panel-bar " align="center">
    <Button @click="confirm" color="primary" v-show="action != 'see'" icon="h-icon-completed"
      :disabled="confirmFlag">确认</Button>&nbsp;&nbsp;&nbsp;&nbsp;
    <Button @click="close" icon="h-icon-close-min">返回</Button>&nbsp;&nbsp;&nbsp;&nbsp;
    <Button @click="reset" color="yellow" v-show="action == 'query'" icon="h-icon-refresh">重置</Button>
  </div>
</template>
<script>
export default {
  name: "PaasButtonBar",
  props: {
    id: {
      default: "id",
    },
    callback: {
      default: null,
      type: function (data) { }
    },
    add: "",
    edit: "",
    autoPk: {
      default: true
    },
  },
  created() {
    if (!!this.add) {
      this.addUrl = this.add;
    } else {
      this.addUrl = this.$store.menus.current;
    }
    if (!!this.edit) {
      this.editUrl = this.edit;
    } else {
      this.editUrl = this.$store.menus.current + "/edit";
    }
  },
  data() {
    return {
      addUrl: "",
      editUrl: "",
      confirmFlag: false,
    };
  },
  computed: {
    action() {
      return this.$store.forms.action;
    },
  },
  methods: {
    confirm() {
      if (this.confirmFlag) {
        return;
      } else {
        this.confirmFlag = true;
        setTimeout(() => {
          this.confirmFlag = !this.confirmFlag;
        }, 2000);
      }
      if (this.$parent["validate"] instanceof Function) {
        if (this.$parent["validate"]() == false) {
          return;
        }
      }
      let validResult = this.$parent.$refs.form.valid();
      if (!validResult.result) {
        return;
      }
      if (this.action == "add") {
        this.addClick();
      } else if (this.action == "edit") {
        this.editClick();
      } else if (this.action == "query") {
        this.query();
      }
    },
    query() {
      let data = this.$parent.model;
      this.$store.setQueryData(JSON.parse(JSON.stringify(data)));
      this.$router.back();
    },
    addClick() {
      let data = this.$parent.model;
      if (this.autoPk) {
        data.id = "";
      }
      this.$post(this.addUrl, data).then(res => {
        let data = res.data;
        if (!!this.callback) {
          this.callback(data);
        } else {
          this.$dyAlert(data, true)
        }

      });
    },
    editClick() {
      let data = this.$parent.model;
      this.$put(this.editUrl, data).then(res => {
        let data = res.data;
        if (!!this.callback) {
          this.callback(data);
        } else {
          this.$dyAlert(data, true)
        }
      });
    },
    close() {
      this.$router.back();
    },
    reset() {
      this.resetData(this.$parent.model);
      this.$store.setQueryData({});
    },
    resetData(obj) {
      for (let key in obj) {
        if (typeof obj[key] === 'object' && obj[key] !== null) {
          obj[key] = (Array.isArray(obj[key])) ? [] : {};
          this.resetData(obj[key]);
        } else {
          switch (typeof obj[key]) {
            case 'string':
              obj[key] = '';
              break;
            case 'number':
              obj[key] = 0;
              break;
            case 'boolean':
              obj[key] = false;
              break;
          }
        }
      }
    }
  },
};
</script>